package org.dev.vlp.controller;

import com.alibaba.fastjson.JSONObject;
import org.dev.vlp.bean.JsonResponse;
import org.dev.vlp.constants.Roles;
import org.dev.vlp.dto.UserLoginParam;
import org.dev.vlp.entity.Community;
import org.dev.vlp.entity.Tag;
import org.dev.vlp.interceptor.RequiredPermission;
import org.dev.vlp.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Description
 * Param
 * return
 * Author:86184
 * Date: 2023/1/8
 */
@RestController
@RequestMapping("/tag")
@RequiredPermission({Roles.ROLE_USER})
public class TagController extends BaseController{

    @Autowired
    private TagService tagService;

    @PutMapping
    public JsonResponse<Object> insert(@RequestBody @Valid Tag tag) {
        tagService.createTag(tag,(int)getUid());
        return JsonResponse.success();
    }

    @PostMapping
    public JsonResponse<Object> update(@RequestBody Tag tag) {
        tagService.modifyTag(tag,(int)getUid());
        return JsonResponse.success();
    }

    @GetMapping("/{tagId}")
    public JsonResponse<List<Tag>> selectTags(@PathVariable("tagId") int tagId) {
        List<Tag> tags=tagService.getBelowTags(tagId,(int)getUid());
        return JsonResponse.success(tags);
    }

    @DeleteMapping
    public JsonResponse<Object> delete(@RequestBody JSONObject jsonObject) {
        List<Integer> tagIds = (List<Integer>) jsonObject.get("tagIds");
        tagService.deleteTag(tagIds,(int)getUid());
        return JsonResponse.success();
    }

}
